package com.yimi.yinhepda.utils;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.pda.uce.commom.util.MyDialog;
import com.yimi.yinhepda.R;

/**
 * @author 000432
 * @date 2018/5/28 0028
 */

public class ExitDialog extends Dialog {

  protected EditText mExitPwd;
  private Button mPositiveBtn;
  private Button mNegativeBtn;
  /** 确定按钮的文字 */
  private String mPositiveText = null;
  /** 取消按钮的文字 */
  private String mNegativeText = null;
  /** 确定按钮背景图id */
  private int btnPositiveId = -1;
  /** 取消按钮背景图id */
  private int btnNegativeId = -1;
  /** 确定按钮监听器 */
  private OnClickListener mPositiveBtnClickListener = null;
  /** 取消按钮监听器 */
  private OnClickListener mNegativeBtnClickListener = null;

  public ExitDialog(@NonNull Context context) {
    super(context, R.style.MyDialog);
  }

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_dialog_exit);
    getWindow().getDecorView().setPadding(10, 0, 10, 0);

    mExitPwd =  findViewById(R.id.et_exit_pwd);

    mNegativeBtn =  findViewById(R.id.btn_cancle);
    mPositiveBtn =  findViewById(R.id.btn_ok);

    if (mPositiveText != null) {
      mPositiveBtn.setText(mPositiveText);
    }
    if (mNegativeText != null) {
      mNegativeBtn.setText(mNegativeText);
    }

    if (btnPositiveId != -1) {
      mPositiveBtn.setBackgroundResource(btnPositiveId);
    }
    if (btnNegativeId != -1) {
      mNegativeBtn.setBackgroundResource(btnNegativeId);
    }

    mPositiveBtn.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (mPositiveBtnClickListener != null) {
          mPositiveBtnClickListener.onClick(ExitDialog.this, MyDialog.BUTTON_POSITIVE);
        }
      }
    });

    mNegativeBtn.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        dismiss();
        if (mNegativeBtnClickListener != null) {
          mNegativeBtnClickListener.onClick(ExitDialog.this, MyDialog.BUTTON_NEGATIVE);
        }
      }
    });
  }

  @Override
  public void show() {
    super.show();
    /**
     * 设置宽度全屏，要设置在show的后面
     */
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
    layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;
    getWindow().setAttributes(layoutParams);

  }

  /**
   * 设置肯定按钮
   */
  public void setPositiveClickListener(@NonNull int btnId, @NonNull String msgText, @NonNull OnClickListener positiveBtnClickListener) {
    if (btnId != -1) btnPositiveId = btnId;
    mPositiveText = msgText;
    mPositiveBtnClickListener = positiveBtnClickListener;
  }

  /**
   * 设置否定按钮
   */
  public void setNegativeClickListener(@NonNull int btnId, @NonNull String msgText, @NonNull OnClickListener negativeBtnClickListener) {
    if (btnId != -1) btnNegativeId = btnId;
    mNegativeText = msgText;
    mNegativeBtnClickListener = negativeBtnClickListener;
  }

  public EditText getmExitPwd() {
    return mExitPwd;
  }
}
